package com.chentongwei.spring.annotation.Import.selector;

import com.chentongwei.spring.annotation.Import.entity.Black;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author TongWei.Chen 2018-07-05 15:15:09
 * @Description:
 * @Project spring-annonation
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    /**
     * @param importingClassMetadata：当前类注解信息
     * @param registry：BeanDefinition注册类，把所有需要添加到容器中的bean，调用BeanDefinitionRegistry.registerBeanDefinition手工注册进来
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean red = registry.containsBeanDefinition("com.chentongwei.spring.annotation.Import.entity.Red");
        boolean blue = registry.containsBeanDefinition("com.chentongwei.spring.annotation.Import.entity.Blue");
        if (red && blue) {
            // 指定bean的id以及bean的定义信息
            registry.registerBeanDefinition("black", new RootBeanDefinition(Black.class));
        }
    }
}
